#ifndef CS104_SERVER_H
#define CS104_SERVER_H

#include "cs104_slave.h"
#include "hal_thread.h"
#include "hal_time.h"

#include <QObject>
#include <QMutex>
#include <vector>

struct S_Yc
{
    int addr;
    float value;
    int qc;
};

struct S_Yx
{
    int addr;
    int statu;
    int qc;
};

struct S_Yk
{
    int addr;
    int statu;
    int qc;
    int yx_addr;
};

struct S_Yt
{
    int addr;
    float value;
    int qc;
    int yc_addr;
};

class simple_server:public QObject
{
    Q_OBJECT
         
private:
    /* data */
    CS104_Slave slave;
    CS101_AppLayerParameters alParams;
    CS104_APCIParameters apciParams;

    bool running;
    bool connected;
    bool activated;

    std::vector<S_Yc> m_yc;
    std::vector<S_Yx> m_yx;
    std::vector<S_Yt> m_yt;
    std::vector<S_Yk> m_yk;
    std::vector<S_Ym> m_ym;

    std::vector<S_Yc> m_yc_change;
    std::vector<S_Yx> m_yx_change;
    std::vector<S_Yt> m_yt_change;
    std::vector<S_Yk> m_yk_change;
    std::vector<S_Ym> m_ym_change;

    QMutex m_mutex;

private:

    void sendYC();
    void sendYX();
    void sendYM();
    void sendYK();
    void sendYT();

public:
    simple_server(const char* addr = "0.0.0.0", int port = 2404);
    ~simple_server();

    void initHandle();
    void start();
    void stop();

    bool isRunning() { return running; };
    bool isConnected() { return connected; };
    bool isActivated() { return activated; };

    void setConnected(bool statu) { connected = statu; };
    void setActivated(bool statu) { activated = statu; };

    int setYC(std::vector<S_Yc> points) { m_yc = points;  return 0; };
    int setYX(std::vector<S_Yx> points) { m_yx = points;  return 0; };
    int setYK(std::vector<S_Yk> points) { m_yk = points;  return 0; };
    int setYT(std::vector<S_Yt> points) { m_yt = points;  return 0; };

    std::vector<S_Yc>* yc() { return &m_yc; };
    std::vector<S_Yx>* yx() { return &m_yx; };
    std::vector<S_Yk>* yk() { return &m_yk; };
    std::vector<S_Yt>* yt() { return &m_yt; };

    int sendYC(int addr, float v, int qc);
    int sendYXSingle(int addr, int v, int qc);
    int sendYXDouble(int addr, int v, int qc);

};

#endif
